/**
 *    Copyright (C) 2007-2009 DIVIS GmbH (http://www.divis-hagen.de/)
 * 
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 3 of the License, or (at your option) any later version.
 * 
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 * 
 *    You should have received a copy of the GNU Lesser General
 *    Public License along with this library; if not, write to the
 *    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *    Boston, MA 02110-1301 USA
 */

package de.divis_hagen.json;

import de.divis_hagen.json.jsonorg.JSONEscape;



/**
 *
 * @author Aike J Sommer
 */
public class JSONContextImpl extends JSONContext {

    private boolean unwrap = false;
    private boolean escapeNames = true;

    public JSONContextImpl() {
    }

    public Marshaller createMarshaller() {
        return new MarshallerImpl(this);
    }

    public String toIdentifier(String name) {
        if (escapeNames) {
            return name;
        }

        String[] parts = name.split("-");
        String result = null;
        for (int i = 0; i < parts.length; i++) {
            String part = parts[i];
            if (result == null) {
                result = part;
            } else {
                result += part.substring(0, 1).toUpperCase() + part.substring(1);
            }
        }

        if (result != null) {
            String realResult = "";
            for (int i = 0; i < result.length(); i++) {
                char c = result.charAt(i);
                if (Character.isLetterOrDigit(c)) {
                    realResult += c;
                }
            }
            return realResult;
        }

        return "";
    }

    @Override
    public void setUnwrap(boolean unwrap) {
        this.unwrap = unwrap;
    }

    @Override
    public boolean isUnwrap() {
        return unwrap;
    }

    @Override
    public boolean isEscapeNames() {
        return escapeNames;
    }

    @Override
    public void setEscapeNames(boolean escapeNames) {
        this.escapeNames = escapeNames;
    }

}
